/**
 * Collection class for holding data
 * about a currently connected player
 */
package user;

import java.sql.ResultSet;
import java.sql.SQLException;

import networkcom.Packet;

import database.SQL;

public class Player {
	private int pid;
	private int x;
	private int y;
	private int rot;
	private int health;
	private int maxhealth;
	private int shield;
	private int maxshield;
	private int energy;
	private int maxenergy;
	private String username;
	private SQL sql;
	private int status;
	protected Packet lastForward;
	protected Packet lastBackward;
	protected Packet lastMLeft;
	protected Packet lastMRight;
	protected Packet lastRLeft;
	protected Packet lastRRight;
	protected long lastupdate;
	
	/**
	 * Build a player from a player id number.
	 * This is done via SQL queries
	 * @param pid - id of the player
	 */
	public Player(int pid){
		lastForward = null;
		lastBackward = null;
		lastMLeft = null;
		lastMRight = null;
		lastRLeft = null;
		lastRRight = null;
		sql = SQL.getInstance();
		ResultSet playerdata = sql.query("SELECT * FROM chardata WHERE pid="+pid);
		try {
			playerdata.next();
			x = playerdata.getInt("x");
			y = playerdata.getInt("y");
			rot = playerdata.getInt("rot");
			health = playerdata.getInt("health");
			maxhealth = playerdata.getInt("maxhealth");
			shield = playerdata.getInt("shield");
			maxshield = playerdata.getInt("maxshield");
			energy = playerdata.getInt("energy");
			maxenergy = playerdata.getInt("maxenergy");
			playerdata = sql.query("SELECT username FROM login WHERE id="+pid);
			playerdata.next();
			username = playerdata.getString("username");
		} catch (SQLException e) {
		}
	}
	
	/**
	 * Modifies the health of the current player
	 * negative number to remove health, positive
	 * number to add health
	 * @param delta - amount to change health
	 */
	public void modHealth(int delta){
		if(health+delta<=maxhealth && health+delta>0){
			health+=delta;
		}else if(health+delta>maxhealth){
			health = maxhealth;
		}else if(health+delta<=0){
			die();
		}
	}
	/**
	 * Modifies the energy of the current player
	 * negative number to remove energy, positive
	 * number to add energy
	 * @param delta - amount to change energy
	 */
	public void modEnergy(int del){
		if(energy+del<=maxenergy && energy+del>0){
			energy += del;
		}else if(energy+del>maxenergy){
			energy = maxenergy;
		}else if(energy+del<=0){
			energy=0;
		}
	}
	/**
	 * Modifies the shield of the current player
	 * negative number to remove shield, positive
	 * number to add shield
	 * @param delta - amount to change shield
	 */	
	public void modShield(int del){
		if(shield+del<=maxshield && shield+del>0){
			shield += del;
		}else if(shield+del>maxshield){
			shield = maxshield;
		}else if(shield+del<0){
			modHealth(shield+del);
			shield = 0;
		}
	}
	/**
	 * Modifies the rotation of the current player
	 * negative number to rotate left, positive
	 * number to rotate right
	 * @param delta - amount to change rotation
	 */	
	public void modRot(int del){
		if(rot+del>360){
			rot = rot+del-360;
		}else if(rot+del<0){
			rot = rot+del+360;
		}else {
			rot+=del;
		}
	}
	
	/**
	 * Modifies the current status of the ship
	 * (i.e. emp, etc)
	 * @param delta
	 */
	public void modStatus(int delta){
		if((status & delta)==0){
			status+=delta;
		}
	}
	
	/**
	 * Kills the current player
	 */
	public void die(){
		//todo implement dying
	}
	
	public void update(){
		if(lastForward!=null){
			if(lastForward.getTime()<lastupdate){
				
			}else{
				
			}
		}
	}
	
	//***************************************
	//* BEGIN GETTERS/SETTERS
	//***************************************
	public int getHealth(){
		return health;
	}
	
	public int getMaxHealth(){
		return maxhealth;
	}
	
	public int getEnergy(){
		return energy;
	}
	
	public int getMaxEnergy(){
		return maxenergy;
	}
	
	public int getSheild(){
		return shield;
	}
	
	public int getMaxShield(){
		return maxshield;
	}
	
	public String getUsername(){
		return username;
	}
	
	public int getX(){
		return x;
	}
	
	public int getY(){
		return y;
	}
	
	public int getPID(){
		return pid;
	}
	
	public int getRot(){
		return rot;
	}
	
	public void setRot(int rot){
		this.rot = rot;
	}
	public void setX(int x){
		this.x = x;
	}
	
	public void setY(int y){
		this.y = y;
	}
	
	public void setHealth(int del){
		health = del;
	}
	
	public void setMaxHealth(int del){
		maxhealth = del;
	}
	
	public void setEnergy(int del){
		energy = del;
	}
	
	public void setMaxEnergy(int del){
		maxenergy = del;
	}
	
	public void setShield(int del){
		shield = del;
	}
	
	public void setMaxShield(int del){
		maxshield = del;
	}
}
